<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Tools\Func;
use App\Models\Baogao;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;


class BaogaoController extends Controller
{
    use Func;

    // 列表
    public function list(Request $request)
    {
        $keywords = trim($request->keywords);
        $data = Baogao::with('jiuzhenren')
		->where('admin_id', $request->user_id)
		->orderBy('id', 'desc')
		->paginate($request->limit ?? 10);
        return $this->returnJson(0, 'success', $data);
    }

    // 添加
    public function add(Request $request)
    {
        if (!$res = Baogao::create([
            'title' => $request->title,
			'content' => $request->content,
			'jiuzhenren_user_id' => $request->jiuzhenren_user_id,
			'jiuzhenren_id' => $request->jiuzhenren_id,
			'admin_id' => $request->user_id,
            'created_at' => date('Y-m-d H:i:s')
        ])) {
            return $this->returnJson(1, '添加失败', $res);
        }

        return $this->returnJson(0, '添加成功', $res);
    }
	

    // 删除
    public function del(Request $request)
    {
        if (!Baogao::where('id', $request->id)->delete()) {
            return $this->returnJson(1, '删除失败');
        }

        return $this->returnJson(0, '删除成功');
    }

    // 获得
    public function get(Request $request)
    {
		// dump($request->all());exit;
        $res = Baogao::where('id', $request->id)->first();

        return $this->returnJson(0, 'success', $res);
    }

    // 编辑
    public function edit(Request $request)
    {
        // dd($request->addAccount);
        if (!$res = Baogao::where('id', $request->id)->update([
            'title' => $request->title,
			'jiuzhenren_id' => $request->jiuzhenren_id,
			'content' => $request->content,	
			
        ])) {
            return $this->returnJson(1, '修改失败', $res);
        }

        return $this->returnJson(0, '修改成功', $res);       
    }

}
